﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CmsField
    {
        public int Add(KeRui.Model.CmsField model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@FieldID", SqlDbType.Int, 4), new SqlParameter("@ModelID", SqlDbType.Int, 4), new SqlParameter("@FieldType", SqlDbType.Int, 4), new SqlParameter("@FieldName", SqlDbType.VarChar, 50), new SqlParameter("@FieldAlias", SqlDbType.NVarChar, 50), new SqlParameter("@Tips", SqlDbType.NVarChar, 200), new SqlParameter("@Description", SqlDbType.NText), new SqlParameter("@DefaultValue", SqlDbType.NVarChar, 500), new SqlParameter("@MaxValue", SqlDbType.VarChar, 20), new SqlParameter("@StyleValue", SqlDbType.NText), new SqlParameter("@EnableNull", SqlDbType.Bit, 1), new SqlParameter("@EnableSearch", SqlDbType.Bit, 1), new SqlParameter("@IsUsing", SqlDbType.Bit, 1), new SqlParameter("@IsTab", SqlDbType.Int) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.ModelID;
            parameters[2].Value = model.FieldType;
            parameters[3].Value = model.FieldName;
            parameters[4].Value = model.FieldAlias;
            parameters[5].Value = model.Tips;
            parameters[6].Value = model.Description;
            parameters[7].Value = model.DefaultValue;
            parameters[8].Value = model.MaxValue;
            parameters[9].Value = model.StyleValue;
            parameters[10].Value = model.EnableNull;
            parameters[11].Value = model.EnableSearch;
            parameters[12].Value = model.IsUsing;
            parameters[13].Value = model.IsTab;
            DbHelperSQL.RunProcedure("CmsField_ADD", parameters, out num);
            return (int) parameters[0].Value;
        }

        public void Delete(int FieldID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@FieldID", SqlDbType.Int, 4) };
            parameters[0].Value = FieldID;
            DbHelperSQL.RunProcedure("CmsField_Delete", parameters, out num);
        }

        public KeRui.Model.CmsField GetModel(int FieldID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@FieldID", SqlDbType.Int, 4) };
            parameters[0].Value = FieldID;
            KeRui.Model.CmsField field = new KeRui.Model.CmsField();
            DataSet set = DbHelperSQL.RunProcedure("CmsField_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["FieldID"].ToString() != "")
                {
                    field.FieldID = int.Parse(set.Tables[0].Rows[0]["FieldID"].ToString());
                }
                if (set.Tables[0].Rows[0]["ModelID"].ToString() != "")
                {
                    field.ModelID = int.Parse(set.Tables[0].Rows[0]["ModelID"].ToString());
                }
                if (set.Tables[0].Rows[0]["FieldType"].ToString() != "")
                {
                    field.FieldType = int.Parse(set.Tables[0].Rows[0]["FieldType"].ToString());
                }
                field.FieldName = set.Tables[0].Rows[0]["FieldName"].ToString();
                field.FieldAlias = set.Tables[0].Rows[0]["FieldAlias"].ToString();
                field.Tips = set.Tables[0].Rows[0]["Tips"].ToString();
                field.Description = set.Tables[0].Rows[0]["Description"].ToString();
                field.DefaultValue = set.Tables[0].Rows[0]["DefaultValue"].ToString();
                field.MaxValue = set.Tables[0].Rows[0]["MaxValue"].ToString();
                field.StyleValue = set.Tables[0].Rows[0]["StyleValue"].ToString();
                if (set.Tables[0].Rows[0]["EnableNull"].ToString() != "")
                {
                    if ((set.Tables[0].Rows[0]["EnableNull"].ToString() == "1") || (set.Tables[0].Rows[0]["EnableNull"].ToString().ToLower() == "true"))
                    {
                        field.EnableNull = true;
                    }
                    else
                    {
                        field.EnableNull = false;
                    }
                }
                if (set.Tables[0].Rows[0]["EnableSearch"].ToString() != "")
                {
                    if ((set.Tables[0].Rows[0]["EnableSearch"].ToString() == "1") || (set.Tables[0].Rows[0]["EnableSearch"].ToString().ToLower() == "true"))
                    {
                        field.EnableSearch = true;
                    }
                    else
                    {
                        field.EnableSearch = false;
                    }
                }
                if (set.Tables[0].Rows[0]["IsUsing"].ToString() != "")
                {
                    if ((set.Tables[0].Rows[0]["IsUsing"].ToString() == "1") || (set.Tables[0].Rows[0]["IsUsing"].ToString().ToLower() == "true"))
                    {
                        field.IsUsing = true;
                    }
                    else
                    {
                        field.IsUsing = false;
                    }
                }
                if (set.Tables[0].Rows[0]["IsTab"].ToString() != "")
                {
                    field.IsTab = int.Parse(set.Tables[0].Rows[0]["IsTab"].ToString());
                }
                if (set.Tables[0].Rows[0]["OrderID"].ToString() != "")
                {
                    field.OrderID = int.Parse(set.Tables[0].Rows[0]["OrderID"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    field.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["UpdateTime"].ToString() != "")
                {
                    field.UpdateTime = DateTime.Parse(set.Tables[0].Rows[0]["UpdateTime"].ToString());
                }
                return field;
            }
            return null;
        }

        public void ModifyIsUsing(int fieldID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@FieldID", SqlDbType.Int, 4) };
            parameters[0].Value = fieldID;
            DbHelperSQL.RunProcedure("CmsField_ModIsUsing", parameters);
        }

        public void Update(KeRui.Model.CmsField model)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@FieldID", SqlDbType.Int, 4), new SqlParameter("@ModelID", SqlDbType.Int, 4), new SqlParameter("@FieldType", SqlDbType.Int, 4), new SqlParameter("@FieldName", SqlDbType.VarChar, 50), new SqlParameter("@FieldAlias", SqlDbType.NVarChar, 50), new SqlParameter("@Tips", SqlDbType.NVarChar, 200), new SqlParameter("@Description", SqlDbType.NText), new SqlParameter("@DefaultValue", SqlDbType.NVarChar, 500), new SqlParameter("@MaxValue", SqlDbType.VarChar, 20), new SqlParameter("@StyleValue", SqlDbType.NText), new SqlParameter("@EnableNull", SqlDbType.Bit, 1), new SqlParameter("@EnableSearch", SqlDbType.Bit, 1), new SqlParameter("@IsUsing", SqlDbType.Bit, 1), new SqlParameter("@IsTab", SqlDbType.Int) };
            parameters[0].Value = model.FieldID;
            parameters[1].Value = model.ModelID;
            parameters[2].Value = model.FieldType;
            parameters[3].Value = model.FieldName;
            parameters[4].Value = model.FieldAlias;
            parameters[5].Value = model.Tips;
            parameters[6].Value = model.Description;
            parameters[7].Value = model.DefaultValue;
            parameters[8].Value = model.MaxValue;
            parameters[9].Value = model.StyleValue;
            parameters[10].Value = model.EnableNull;
            parameters[11].Value = model.EnableSearch;
            parameters[12].Value = model.IsUsing;
            parameters[13].Value = model.IsTab;
            DbHelperSQL.RunProcedure("CmsField_Update", parameters);
        }
    }
}

